//
//  ORNewsTableViewController.m
//  News
//
//  Created by rimi on 2016/12/11.
//  Copyright © 2016年 dongfumei. All rights reserved.
//

#import "ORNewsTableViewController.h"
#import "ORTouModel.h"
#import "ORTouTableViewCell.h"
#import "ORChannelContentViewController.h"

@interface ORNewsTableViewController ()

@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation ORNewsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializeAppearence];
    [self.tableView registerClass:[ORTouTableViewCell class] forCellReuseIdentifier:@"ORTouTableViewCell"];
    
}

-(void)initializeAppearence{
//    NSLog(@"%@",self.type);
    NSDictionary *dic = @{@"key":@"6aa0243488ef2da834a378beabca0b25",@"type":_type};
    
    __weak typeof(self) weakSelf = self;
    [ORNetWorkRequestManager getMethod:@"http://v.juhe.cn/toutiao/index" withParames:dic Success:^(id respondObject) {
//        NSLog(@"%@",respondObject);
        
        NSArray *dataArr = respondObject[@"result"][@"data"];
        if ([dataArr isKindOfClass:[NSArray class]]) {
            _dataSource = [ORTouModel buildNews:dataArr];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
            });
        }
    } Failure:^(NSError *error) {
        
    }];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ORTouTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ORTouTableViewCell" forIndexPath:indexPath];
    
    [cell withDataModel:_dataSource[indexPath.row]];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ORTouModel *model = _dataSource[indexPath.row];
    CGFloat height = [ORTouTableViewCell cellHeight:model.title];
    return height+10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ORChannelContentViewController *contentVC = [[ORChannelContentViewController alloc]init];
    ORTouModel *model = _dataSource[indexPath.row];
    contentVC.url = model.url;;
    contentVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:contentVC animated:YES];
    contentVC.hidesBottomBarWhenPushed = NO;
}

@end
